<script setup>
import { reactive, ref } from 'vue'
import { myConfrim } from '@/utils'
import { getListApi, applyDoApi } from '@/api/mybed'
import { useUserStore } from '@/store'

const userStore = useUserStore()

const userId = userStore.userId
const userType = userStore.userType

const tableData = ref([])
const parm = reactive({
    currentPage: 1,
    pageSize: 10,
    userId: userId,
    userType: userType,
    total: 0
})

// 处理请求
const applyDo = async (type, applyId) => {
    let res = await applyDoApi({
        type,
        applyId,
        userType: userType
    })
    if (res) {
        ElMessage({
            message: res.msg,
            type: 'success',
        })
        getList()
    }
}
const getList = async () => {
    let res = await getListApi(parm)
    if (res) {
        tableData.value = res.data.records
        parm.total = res.data.total
    }
}

getList()
// 同意
const applyBtn = async (row) => {
    if (row.status !== '0') {
        ElMessage({
            message: '该申请已处理',
            type: 'warning',
        })
        return
    }
    let confirm = await myConfrim('确定同意吗?')
    if (confirm) {
        applyDo('1', row.applyId)
    }
}
// 拒绝
const returnBtn = async (row) => {
    if (row.status !== '0') {
        ElMessage({
            message: '该申请已处理',
            type: 'warning',
        })
        return
    }
    let confirm = await myConfrim('确定同意吗?')
    if (confirm) {
        applyDo('2', row.applyId)
    }
}
</script>

<template>
    <el-table :data="tableData" border stripe>
        <el-table-column prop="applyUserName" label="申请人"></el-table-column>
        <el-table-column prop="applyBedCode" label="申请人床位"></el-table-column>
        <el-table-column prop="changeUserName" label="调换人"></el-table-column>
        <el-table-column prop="changeBedCode" label="调换床位"></el-table-column>
        <el-table-column prop="status" label="申请状态">
            <template #default="scope">
                <el-tag v-if="scope.row.status === '2'" type="danger">拒绝</el-tag>
                <el-tag v-if="scope.row.status === '1'" type="success">同意</el-tag>
                <el-tag v-if="scope.row.status === '0'" type="warning">审核中</el-tag>
            </template>
        </el-table-column>
        <el-table-column v-if="userType === '1'" v-permission="['sys:applyList:apply', 'sys:applyList:return']" fixed="right" label="操作"
            align="center" width="220">
            <template #default="scope">
                <el-button icon="Edit" type="primary" @click="applyBtn(scope.row)">同意</el-button>
                <el-button icon="Delete" type="danger" @click="returnBtn(scope.row)">拒绝</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>

